package com.book.controller.system;

import com.book.core.domain.SysRole;
import com.book.core.dto.SysUserGrantRoleDTO;
import com.book.core.mode.R;
import com.book.core.query.SysRoleQuery;
import com.book.core.vo.PageVO;
import com.book.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class SysRoleController {
    private final SysRoleService sysRoleService;

    @GetMapping("/page")
    public R<PageVO<SysRole>> getSysRolePage(SysRoleQuery query) {
        PageVO<SysRole> page = sysRoleService.getSysRolePage(query);
        return R.ok(page);
    }

    @PostMapping
    public R save(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return R.ok("新增成功",null);
    }

    @PutMapping
    public R update(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return R.ok("修改成功",null);
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        sysRoleService.removeById(id);
        return R.ok("删除成功",null);
    }
    @GetMapping("/list/by/user/{userId}")
    public R<List<SysRole>> getRoleIdListByUserId(@PathVariable Integer userId) {
        List<SysRole> roleList = sysRoleService.getRoleListByUserId(userId);
        return R.ok(roleList);
    }
    @PostMapping("/grant")
    public R<Void> grantUserToRole(@RequestBody @Validated SysUserGrantRoleDTO dto) {
        sysRoleService.grantUserToRole(dto);
        return R.ok("授权成功",null);
    }
}
